import request from '@/utils/request'
import {
	decryptedDes

} from "@/utils/crypto/encrypt-rsa";

import {
	getCookies,
	setCookiesByExpiresTime
} from "@/utils/cookies";

import {
	encryptedRsa
} from "@/utils/crypto/encrypt-rsa";

import {
	isNull
} from "@/utils/validate";


// 登录方法
export async function login(data1) {
	if (true) {
		// 获得公钥

		const publicKey = await getPublicKeyC()

		let encrypted = encryptedRsa(data1, publicKey);

		return request({
			url: "/system/login",
			method: "post",
			data: {
				username: data1.principal,
				encryptData: encrypted,
			},
		});
	}
}


// 获取位置信息
export function getMap(data1) {
	console.log("发起请求");
	return request({
		url: "/weMini/NotToken/Map/get",
		method: "get",
		data: data1,
	});
}


// 小程序登录方法
export function weMiniLogin(data1) {
	return request({
		url: "/weMini/login",
		method: "get",
		data: {
			code: data1,
		},
	});
}

// 公众号登录方法
export function WeChatMpLogin(data) {
	return request({
		url: "/weChatMp/login",
		method: "get",
		data: data,
	});
}

// 小程序获取手机号
export function getPhoneByWeChatCode(data) {
	return request({
		url: "/weMini/NotToken/getPhoneByWeChatCode",
		method: 'get',
		data: data,
	});
}

/**
 * 获取公钥
 */
export function getPublicKey() {
	return request({
		url: "/api/v1/common/public-key",
	})
}
const COOKIE_KEY = "public_key";

export async function getPublicKeyC() {
	let publicKey = getCookies(COOKIE_KEY);

	// 如果公钥为空 则去接口查询
	if (1 == 1) {


		const {
			data
		} = await getPublicKey()
		publicKey = data
		// 存储 公钥到 Cookies 默认存储时长为5分钟
		setCookiesByExpiresTime(COOKIE_KEY, publicKey, 300);
	}

	return publicKey;
};

export function createGetOpenIdUrl(data) {
	return request({
		url: "/weChat/createGetOpenIdUrl",
		data
	})
}
export function callBackUrl(data) {
	return request({
		url: "/h5/callBackUrl",
		data
	})
}

export function ddGetUserByCode(data) {
	return request({
		url: "/wxAuth/ddGetUserByCode",
		data
	})
}
export function getAccess_tokenAdnOpenidByCode(data) {
	return request({
		url: "/weChat/getOpenidByCode",
		data
	})
}



// 注册方法
export function register(data) {
	return request({
		url: '/register',
		headers: {
			isToken: false
		},
		method: 'post',
		data: data
	})
}

// 获取用户详细信息
export function getInfo() {
	return request({
		'url': '/api/v1/system/user/getInfo?type=H5',
		'method': 'get'
	})
}

export function findMenuTree() {
	return request({
		url: "/api/v1/system/menu/findMenuTree",
		method: "post",
	});
}

// 退出方法
export function logout() {
	return request({
		'url': '/system/logout',
		method: 'post'
	})
}

// 获取验证码
export function getCodeImg() {
	return request({
		'url': '/captchaImage',
		headers: {
			isToken: false
		},
		method: 'get',
		timeout: 20000
	})
}


// 获取验证码
export function geocoder(data) {
	return request({
		url: 'http://apis.map.qq.com/ws/geocoder/v1',
		headers: {
			isToken: false
		},
		data: data,
		method: 'get',
		timeout: 20000
	})
}